package com.atguigu.blog.controller;

import com.atguigu.blog.pojo.Category;
import com.atguigu.blog.service.CategoryService;
import com.atguigu.blog.util.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @ClassName CategoryController
 * @Description TODO
 * @Author 孤妤
 * @Date 2023/11/24 16:50
 */
@RestController
@RequestMapping("category")
@CrossOrigin
@Tag(name = "文章分类模块")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;
    @GetMapping
    @Operation(summary = "查询文章分类标签")
    public Result<List<Category>> showAll(){
        List<Category> categoryList = categoryService.showAll();
        return Result.ok(categoryList);
    }
    @PostMapping("{page}/{limit}")
    @Operation(summary = "查询文章分页列表")
    @Parameters({@Parameter(name = "page",description = "分页页码",required = true),
                @Parameter(name = "limit",description = "分页页数",required = true),
                @Parameter(name = "category",description = "搜索关键字")})
    public Result<Map<String,Object>> showAllPage(@PathVariable Integer page,
                                                  @PathVariable Integer limit,
                                                  @RequestBody Category category){
        Map<String, Object> map = categoryService.showAllPage(page, limit, category);
        return Result.ok(map);
    }
    @GetMapping("{cid}")
    @Operation(summary = "查询分类名称")
    @Parameters(@Parameter(name = "cid",description = "分类cid",required = true))
    public Result<Category> showAllByCid(@PathVariable Integer cid){
        Category category = categoryService.showAllByCid(cid);
        return Result.ok(category);
    }
    @PostMapping
    @Operation(summary = "添加文章分类名称")
    @Parameters(@Parameter(name = "category",description = "分类名称",required = true))
    public Result<Map<String,Object>> showAllPage(@RequestBody Category category){
        Integer result = categoryService.add(category);
        if (result>0){
            return Result.ok();
        }
        return Result.fail();
    }
    @PutMapping
    @Operation(summary = "修改文章分类名称")
    @Parameters(@Parameter(name = "category",description = "分类名称"))
    public Result<Integer> change(@RequestBody Category category){
        Integer result = categoryService.change(category);
        if (result>0){
            return Result.ok();
        }
        return Result.fail();
    }
    @DeleteMapping("{cid}")
    @Operation(summary = "删除文章分类名称")
    @Parameters(@Parameter(name = "cid",description = "分类cid"))
    public Result<Integer> remove(@PathVariable Integer cid){
        Integer result = categoryService.remove(cid);
        if (result>0){
            return Result.ok();
        }
        return Result.fail();
    }
}